/**
 *名单管理页面js
 * 版权所有：上海戈品投资管理有限公司
 *   author king
     功能说明：名单管理
     创建日期：2015-10-12
     修改记录：
     备注说明：
 */

Date.prototype.format =function(format)
    {
        var o = {
        "M+" : this.getMonth()+1, //month
		"d+" : this.getDate(),    //day
		"h+" : this.getHours(),   //hour
		"m+" : this.getMinutes(), //minute
		"s+" : this.getSeconds(), //second
		"q+" : Math.floor((this.getMonth()+3)/3),  //quarter
		"S" : this.getMilliseconds() //millisecond
        }
        if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
        (this.getFullYear()+"").substr(4- RegExp.$1.length));
        for(var k in o)if(new RegExp("("+ k +")").test(format))
        format = format.replace(RegExp.$1,
        RegExp.$1.length==1? o[k] :
        ("00"+ o[k]).substr((""+ o[k]).length));
        return format;
 };

$(document).ready(function() {
	showRollList();
	 $(".date").datepicker({
	        todayBtn: "linked",
	        keyboardNavigation: false,
	        forceParse: false,
	        calendarWeeks: true,
	        autoclose: true
	    });
});

/**
 * 查询快递员站点
 * @returns
 */
var showRollList = function(){

    var reissueQuery=$("#selectList").val();
    var userType=$("#userType").val();//用户类型
    var userId = $("#userId").val();
	var name_wx = $("#name_wx").val();
	var real_name = $("#real_name").val();
	
	 var datas =  {"p":"rollManage","m":"q","userType":userType,"userId":userId,"name_wx":name_wx,"real_name":real_name,"type":reissueQuery};
	 var columnDefine = {"id":"编号","rollupatetime":"更新日期","name_wx":"昵称","real_name":"真实姓名","telephone":"手机号","company":"快递公司","userType":"用户类型","rollStatus":"状态","yellowRestoretime":"解封日期","ext1":"操作日志"};
	 var formatters={
	            "operLogs": function (column, rowData)
	            {
	            	if( rowData.ext1 )
	            	{
		            	var str= new Array();
	        			str = (rowData.ext1).split("|");
	        			var info='';
	        			for(var i=0;i<str.length-1;i++){
	        				info += i+1+':'+str[i]+'<br/>';
	        			}
	        			return info;
	            	}
	            	else
	            	{
	            		return "";
	            	}	

	            },
	            "userType": function (column, rowData)
	            {
    				if(rowData.score==""||rowData.score==null){
    					return "";
    				}
    				var cpj="";
    				if(rowData.userType==1)
    				{
    					cpj="快递员";
    				}
	                else
	                {
	                	cpj="用户";
    			    }
    				return cpj;
	            },
	            "rollStatus": function (column, rowData)
	            {
    				if(rowData.rollStatus==1)
    				{
    					return "黄名单";
    				}
    				else if(rowData.rollStatus==2)
    				{
    					return "红名单";
    				}
    				else if(rowData.rollStatus==3)
    				{
    					return "黑名单";
    				}
	            }
	 };
	 var buttons =''; //操作按钮
		buttons+='<p>';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-info\' onclick="showInfo();">详情</button>';
		buttons+='</p>';
	hui.pager('service',datas ,"rollList", columnDefine , formatters , buttons ); //分页查询
	 
};

var getTheSureTime=function(emp){
	return emp>9?emp:"0"+emp;
}
//时间格式转换
var timeFormatTrans=function(time){
	var newTime= new Date(time);
	var rowText=newTime.getFullYear()+'-'+getTheSureTime((newTime.getMonth()+1))+'-'
	+getTheSureTime(newTime.getDate())+' '+getTheSureTime(newTime.getHours())+':'
	+getTheSureTime(newTime.getMinutes())+':'+getTheSureTime(newTime.getSeconds());
	return rowText;
};

/**
 * 打开设置窗口
 * @returns
 */
var openSetupWin = function(){
	$('#setupWin input').val('');
	var newTime = new Date();
	var rowText=newTime.getFullYear()+'-'+getTheSureTime((newTime.getMonth()+1))+'-'+getTheSureTime(newTime.getDate());
	$('#setupWin .updateTime').val(rowText);
	$('#setupWin').modal('show');
};

/**
 * 显示详情
 * @returns
 */
var showInfo = function(){
	var datas = $('#pop_win .data').text();
	var json = eval('('+datas+')');
	var id = json.id;
	window.location.href='page?p=page/rollDetailInfo&id='+id+'&date=';
};

/**
 * 设置状态（重置/申诉）
 * @param flag 1 重置 2 申诉
 * @returns
 */
var setup = function( flag ){
	
	var allList=$("#allSelectList").is(":checked")?1:0;//全选状态
	var datas={};
	var ids="";
	
	var status=$("#setupWin .status").val();
	var type=$("#setupWin .userType").val();
	var updateTime = $("#setupWin .updateTime").val();
	
	if( status == '' || status == null || status == undefined )
	{
		common.showMsg('error', '', '请请选择名单状态');
		return;
	}	
	
	var checks= $('#rollList input[name="select"]:checked');
	
	if( allList == 1 )//全选
	{
	    var reissueQuery=$("#selectList").val();
	    var userId = $("#userId").val();
		var name_wx = $("#name_wx").val();
		var real_name = $("#real_name").val();
	    datas = {"p":"rollManage","m":"statusReset","type":reissueQuery,"userType":type,"updateTime":updateTime,"allList":allList,"flag":flag,"status":status,"userId":userId,"name_wx":name_wx,"name_wx":name_wx,"real_name":real_name};
	}
	else
	{
		if( checks.length == 0 )
		{
			common.showMsg('error', '', '请先在名单列表中选中要设置状态的人');
			return;
		}
		else
		{
			for(var i=0;i<checks.length;i++)
			{
				ids+=checks.eq(i).attr("value")+",";
			}
		}
		datas={"p":"rollManage","m":"statusReset","type":flag,"allList":allList,"updateTime":updateTime,"flag":flag,"status":status,"ids":ids.substring(0,ids.length-1)};
	}	
	
	var json=common.ajaxRequest('service', datas, "post");
	if( json && json.result == 0 )
	{
		showRollList();
		$('#setupWin').modal('hide');
		common.showMsg('info', '', '设置成功');
	}
	else
	{
		common.showMsg('error', '', '设置失败,' + json.message);
		return;
	}
	
};
